package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.UserDao;
import com.bdqn.zhang.pojo.Emp;
import com.bdqn.zhang.pojo.Picture;
import com.bdqn.zhang.pojo.Sysuser;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    @Resource(name = "sessionFactory")
    public void setSessionFactoryOverride(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public Sysuser findUser(String username, String password) {
        List<Sysuser> list = (List<Sysuser>)this.getHibernateTemplate().find("from Sysuser where username = ? and password = ?", username, password);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Emp> getAllEmps(final Integer limit, final Integer page) {

//        List<Emp> list = (List<Emp>)this.getHibernateTemplate().find("from Emp inner join fetch Dept");
        /*List<Emp> list = (List<Emp>)(this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery("from Emp e inner join fetch e.dept").setFirstResult(page).setMaxResults(limit));
        if(list != null && list.size() > 0){
            return list;
        }
        return null;*/
        return this.getHibernateTemplate().execute(new HibernateCallback<List<Emp>>() {
            @Override
            public List<Emp> doInHibernate(Session session) throws HibernateException {
                List<Emp> emps = session.createQuery("from Emp e inner join fetch e.dept").setFirstResult((page-1)*limit).setMaxResults(limit).list();
                return emps;
            }
        });
    }

    @Override
    public Long getCount() {
        return this.getHibernateTemplate().execute(new HibernateCallback<Long>() {
            @Override
            public Long doInHibernate(Session session) throws HibernateException {
                return  (Long) session.createQuery("select count(1) from Emp").list().get(0);
            }
        });
    }

    @Override
    public Long addPicture(Picture picture) {
        return (Long)this.getHibernateTemplate().save(picture);
    }

    @Override
    public List<Picture> getAllPics(final Integer limit, final Integer page) {
        return this.getHibernateTemplate().execute(new HibernateCallback<List<Picture>>() {
            @Override
            public List<Picture> doInHibernate(Session session) throws HibernateException {
                List<Picture> pictures = session.createQuery("from Picture").setFirstResult((page-1)*limit).setMaxResults(limit).list();
                return pictures;
            }
        });
    }

    @Override
    public Long getPicsCount() {
        return this.getHibernateTemplate().execute(new HibernateCallback<Long>() {
            @Override
            public Long doInHibernate(Session session) throws HibernateException {
                return  (Long) session.createQuery("select count(1) from Picture").list().get(0);
            }
        });
    }
}
